﻿menutype = ""; //当前需要绑定的树类型
//页面初始化
$(document).ready(function() {
    //绑定页面右上角链接的点击事件
    $(".menu").click(function() {
        menutype = $(this).attr('name');
        getContextMenu(menutype);
        $("#menu_name").html($(this).attr('alt')); //树形菜单的标题
        //第一次加载的树形结构,同时也绑定了里面树节点的点击事件
        $('#tree').fileTree({ root: $(this).attr('name'), folderEvent: 'click', expandSpeed: 500, collapseSpeed: 500 }, function(file) {
            //当点击"文件"时 执行的操作
            var strUrl = file;
            if (strUrl.toString().indexOf("?", 0) < 0)
                strUrl = strUrl + "?random=" + Math.random();
            else
                strUrl = strUrl + "&random=" + Math.random();
            parent.document.getElementById("right").src = strUrl;
        });
    });
    menutype = $("#menu_1").attr('name'); //默认执行第一个点击事件
    $("#menu_1").click();
    //当tree加载完毕时,加载右键菜单
    $("#tree").ajaxComplete(function(event, request, settings) {
        $("#tree ul li a").each(function() {
            //当a 有 name时则加载右键
            if ($(this).attr("menu") != undefined && $(this).attr("menu") == "true") {
                //alert("wai:"+ $(this).attr("nodeid"));
                addMenuByName($(this));
            }
        });
    });

});
//获取右键菜单
function getContextMenu(strName) {
    $.get("getContextMenu.aspx", { name: strName, strRandom: Math.random() },
      function(data) {
          $('#rightmenu').html("");
          $('#rightmenu').append(data);
      });
}
//刷新事件
function Refresh() {
    $("a[name='" + menutype + "']").click();
}
//删除结点
function deleteNode(nodeid) {
    //alert(nodeid);
    var returntext = $.ajax({
        type: "POST",
        url: 'node/Node_save.aspx',
        data: 'ac=ajax_del&id=' + nodeid,
        async: false
    }).responseText;
    var msg = eval("msg=" + returntext);
    if (msg.result == "1") {
        alert(msg.msg);
        Refresh();
    } else {
        alert(msg.msg);
    }
}
//根据名称加载右键
function addMenuByName(object) {
    var name =object.attr("name");
    //==结点 start
    if (name == "node_children") {
        var thisnodeid = object.attr("nodeid");
        //alert("nei2:" + object.attr("nodeid"));
        object.contextMenu(
            { menu: object.attr("name") + "ID" },
            function(action, el, pos) {
                var strHref = $(action).attr("href");
                if (strHref.indexOf("ac=del") > 0) {
                    if (confirm('你确定要删除这个结点和结点下的子结点?')) {
                        deleteNode(thisnodeid)
                    }
                    return false;
                }
                else {
                    //alert(strHref);
                    parent.document.getElementById("right").src = strHref + thisnodeid;
                }
            }
        );
    }
    //==结点 end
    //==发布 start
    else if (name == "pubnode_children") {
        var thisnodeid = object.attr("nodeid");
        //alert("nei2:" + object.attr("nodeid"));
        object.contextMenu(
            { menu: object.attr("name") + "ID" },
            function(action, el, pos) {
                var strHref = $(action).attr("href");
                if (strHref.indexOf("ac=add") > 0) {
                    going('newdoc', thisnodeid);
                    return false;
                }
                else {
                    //alert(strHref);
                    parent.document.getElementById("right").src = strHref + thisnodeid;
                }
            }
        );
    }
    //==发布 end
    //==其他 start
    else {
        object.contextMenu(
             { menu: object.attr("name") + "ID" },
               function(action, el, pos) {//action 当前<a ...> el 当前父<a....
                       strUrl = action.attr("href"); //menu链接
                       if (strUrl.toString().indexOf("?", 0) < 0)
                           strUrl = strUrl + "?random=" + Math.random();
                       else
                           strUrl = strUrl + "&random=" + Math.random();
                       if ($(el).attr("param") != undefined) {
                           strUrl = strUrl + el.attr("param");
                       }
                       parent.document.getElementById("right").src = strUrl;
               }
        );
    }
    //==其他 end
}
//发布管理右键菜单js方法
function going(t, id) {
    if (t == "newdoc") {//新建文档
        frames["right"].show_dialog('../publish/content_save.aspx?ac=add&nid=' + id, 850, 610, '新建文档');

    } else if (t == "editdoc") {//编辑文档
        frames["right"].show_dialog('../publish/content_save.aspx?ac=edit&id=' + id, 850, 610, '编辑文档');
    }
    else 
    {

    }
return false;
    
}